package com.symaster.backups.frame.element.collapsev2;

import com.formdev.flatlaf.ui.FlatRoundBorder;
import com.symaster.backups.frame.element.icon.RotateIcon;
import com.symaster.backups.util.TimeUtil;
import com.symaster.common.animation.Curve;
import com.symaster.common.swing.util.SwingUtil;
import lombok.Getter;
import lombok.Setter;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @author yinmiao
 * @version 2024/2/21
 */
public class CollapseItem extends JComponent {

    static final float execTime = 200;
    static final Curve c = Curve.TYPE_FAST_TO_SLOW;

    @Getter
    private JComponent top, center;
    @Getter
    @Setter
    private int iconSize = 13;

    boolean run = false;
    float runTime;
    boolean optionType = false; // true:展开, false:收起
    int openHeight;

    final Refresh refresh;
    final RotateIcon rotateIcon;
    final JButton rootBtn;
    final Set<RefreshListener> refreshListeners = new HashSet<>();
    final Set<OpenListener> openListeners = new HashSet<>();

    public void setTop(JComponent top) {
        this.top = top;
        if (top != null) {
            rootBtn.add(top, BorderLayout.CENTER);
        }

    }

    public void setCenter(JComponent center) {
        this.center = center;
        if (center != null) {
            add(center, CollapseItemLayout.CENTER);
            center.setVisible(false);
            rootBtn.add(rotateIcon, BorderLayout.WEST);
        } else {
            rootBtn.remove(rotateIcon);
        }
    }

    public CollapseItem() {
        setBorder(new FlatRoundBorder());

        this.refresh = new Refresh(this);

        ImageIcon imageIcon = SwingUtil.loadIconEnabledCache("triangle.png", iconSize, iconSize);
        this.rotateIcon = new RotateIcon(imageIcon.getImage());
        this.rotateIcon.setAngleOffset(-90);

        this.rootBtn = new JButton();
        // this.rootBtn.setMargin(new Insets(0, this.iconSize, 0, 0));
        this.rootBtn.setBorder(new EmptyBorder(0, this.iconSize, 0, 0));
        this.rootBtn.setLayout(new BorderLayout());
        this.rootBtn.addActionListener(e -> {
            if (!this.run && CollapseItem.this.center != null) {
                this.openListeners.forEach(e1 -> e1.before(!this.optionType));

                if (!this.optionType) {
                    this.openHeight = CollapseItem.this.center.getPreferredSize().height;
                }

                this.runTime = TimeUtil.floatTime();
                this.optionType = !optionType;
                this.run = true;
                CollapseItem.this.center.setVisible(true);
            }
        });

        this.setLayout(new CollapseItemLayout());
        this.add(this.rootBtn, CollapseItemLayout.TOP);
    }

    public CollapseItem(JComponent top, JComponent center) {
        this();
        this.setTop(top);
        this.setCenter(center);
    }

    public void addRefreshListener(RefreshListener l) {
        refreshListeners.add(l);
    }

    public void addOpenListener(OpenListener l) {
        openListeners.add(l);
    }

    @Override
    public void paint(Graphics g) {
        doLayout();

        super.paint(g);

        if (run) {
            SwingUtilities.invokeLater(this.refresh);
        }
    }

    private record Refresh(CollapseItem collapseItem) implements Runnable {

        @Override
        public void run() {
            collapseItem.repaint();
            collapseItem.refreshListeners.forEach(RefreshListener::refresh);
        }
    }

    public interface OpenListener {

        /**
         * 展开之前
         *
         * @param optionType true:展开, false:收起
         */
        void before(boolean optionType);

        /**
         * 展开之后
         *
         * @param optionType true:展开, false:收起
         */
        void after(boolean optionType);
    }

    public interface RefreshListener {
        void refresh();
    }
}
